package fr.nts.application.novadmin.front.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import fr.nts.application.novadmin.service.NovadminUserService;

/**
 * Controlleur MVC lié à l'authentification de l'application.
 * 
 * @author sco
 * @version 1.0.0
 */
@Controller
public class AuthentificationController{
	
    protected static final Logger log = LoggerFactory.getLogger(AuthentificationController.class);

    private final static String LOGIN_VIEW = "/novadmin/login/index";
    
    @Resource
    private NovadminUserService novadminUserService;
    
	@RequestMapping(value = "/secure/login", method = RequestMethod.GET)
	public String login(){
		return LOGIN_VIEW;
	}
	
	@RequestMapping(value = "/secure/action/login/ok", method = RequestMethod.GET)
	public @ResponseBody Boolean ok(){
		return Boolean.TRUE;
	}
	
	@RequestMapping(value = "/secure/action/login/ko", method = RequestMethod.GET)
	public @ResponseBody Boolean ko(){
		return Boolean.FALSE;
	}
	
	@RequestMapping(value = "/secure/action/password/reset", method = RequestMethod.POST)
	public @ResponseBody Boolean resetPassword(@RequestParam(required = true) String login,
			                                   HttpServletRequest request){
		
		// Appel du service de rénit
		try{
			novadminUserService.resetEmployeePassword(login);
		}catch(Exception e){
			log.warn("Erreur dans la réinitialisation du password", e);
		}
		
		return Boolean.TRUE;
	}
}